package com.yugao.fintech.antelope.mps.controller;

import cn.hutool.core.util.RandomUtil;
import com.github.yitter.idgen.YitIdHelper;
import com.yugao.fintech.antelope.mps.api.constatnts.ChannelTypeEnum;
import com.yugao.fintech.antelope.mps.api.model.content.MsgContent;
import com.yugao.fintech.antelope.mps.api.utils.MsgPushUtils;
import com.yugao.fintech.framework.assistant.utils.DateUtils;
import com.yugao.fintech.framework.core.response.Resp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.Duration;
import java.util.Date;

@Api(tags = "推送测试")
@RestController
@RequestMapping("/msg/push")
public class TestMsgPushController {
    @Value("${wx.miniapp.testOpenId:}")
    private String maOpenId;

    @GetMapping("/testPushEmail")
    public Resp<?> testPushEmail(@RequestParam("email") String email) {
        MsgContent content = new MsgContent();
        content.setTaskCode("error_login_email");
        content.setBizKey("1");
        content.setBizType("test");
        content.addTplData("username", "沈光阳")
                .addTplData("date", DateUtils.getDateOfStr(new Date(), "yyyy-MM-dd HH:mm:ss"))
                .addTplData("city", "杭州");
        MsgPushUtils.pushAsync(ChannelTypeEnum.EMAIL, content, email);
        return Resp.ok();
    }

    @GetMapping("/test03")
    public Resp<?> test03() {
        MsgContent content = new MsgContent();
        content.setTaskCode("login_code_email");
        content.setTitle("这是一个测试标题");
        content.setBizKey("1");
        content.setBizType("test");
        content.addTplData("username", "沈光阳")
                .addTplData("operateDescribe", "登录")
                .addTplData("verifyCode", RandomUtil.randomInt(10000, 99999));

        MsgPushUtils.push(ChannelTypeEnum.EMAIL, content, "shenguangyang@foxmail.com");
        return Resp.ok();
    }

    @ApiOperation("短信推送")
    @GetMapping("/testPushSms")
    public Resp<?> testPushSms(@RequestParam("phone") String phone) {
        MsgContent content = new MsgContent();
        content.setTaskCode("test_sms");
        content.setBizKey("1").setBizType("test");
        content.addTplData("value", RandomUtil.randomInt(10000, 99999));
        MsgPushUtils.push(ChannelTypeEnum.SMS, content, phone);
        return Resp.ok();
    }

    @ApiOperation("阿里云短信推送")
    @GetMapping("/testPushAliyunSms")
    public Resp<?> testPushAliyunSms(@RequestParam("phone") String phone) {
        MsgContent content = new MsgContent();
        content.setTaskCode("aliyun_sms");
        content.setBizKey("1").setBizType("test");
        content.addTplData("value", RandomUtil.randomInt(10000, 99999));
        MsgPushUtils.push(ChannelTypeEnum.SMS, content, phone);
        return Resp.ok();
    }

    @ApiOperation("推送微信公众号")
    @GetMapping("/testPushWxMp")
    public Resp<?> testPushWxMp() {
        MsgContent content = new MsgContent();
        content.setTaskCode("wx_mp_test");
//        content.setPageUrl("https://www.baidu.com")
        content.setBizKey("1").setBizType("test");
        content.addTplData("goodsName", "华为mate40pro")
                .addTplData("time", "2020-10-25")
                .addTplData("price", "6999", "#ef5777")
                .addTplData("remark", "麒麟9000牛逼");

        content.pushAfter(Duration.ofSeconds(15));
        MsgPushUtils.pushAsync(ChannelTypeEnum.WX_MP, content, "o63YF52s67WW4SQ8_VkWqVwamN-4");
        return Resp.ok();
    }

    @ApiOperation("微信小程序服务推送")
    @GetMapping("/test06")
    public Resp<?> test06() {
        MsgContent content = new MsgContent();
        content.setTaskCode("wx_mini_app");
        content.setPageUrl("https://www.baidu.com").setBizKey("1").setBizType("test");
        content.addTplData("time4", DateUtils.getDateOfStr(new Date(), "yyyy-MM-dd HH:mm:ss"))
                .addTplData("thing2", "A1098")
                .addTplData("thing3", "前面还有5桌，请耐心等待")
                .addTplData("thing1", "麦当劳下渡路餐厅");

        content.pushAfter(Duration.ofSeconds(10));
        MsgPushUtils.pushAsync(ChannelTypeEnum.WX_MINI_APP, content, maOpenId);
        return Resp.ok();
    }

    @ApiOperation("pc推送")
    @GetMapping("/testPushPC")
    public Resp<?> testPushPC() {
        MsgContent content = new MsgContent();
        content.setTaskCode("order_notice");
        content.setBizKey("1").setBizType("test");
        content.addTplData("orderId", YitIdHelper.nextId());

        MsgPushUtils.push(ChannelTypeEnum.PC, content, "1"); // 用户id
//        MsgPushUtils.push(ChannelTypeEnum.PC, content, "*"); // 给所有用户推送
        return Resp.ok();
    }

    @ApiOperation("推送多通道测试")
    @GetMapping("/pushMultiChannel")
    public Resp<?> pushMultiChannel(@RequestParam("phone") String phone) {
        MsgContent content = new MsgContent();
        content.setTaskCode("order_pay_success");
        content.setTitle("订单支付成功");
        content.setBizKey("1");
        content.setBizType("test");
        content.addTplData("username", "沈光阳")
                .addTplData("operateDescribe", "登录")
                .addTplData("verifyCode", RandomUtil.randomInt(10000, 99999));

        content.addTplData("goodsName", "华为mate40pro")
                .addTplData("time", "2020-10-25")
                .addTplData("price", "6999", "#ef5777")
                .addTplData("remark", "麒麟9000牛逼");

        // 推送邮件
        MsgPushUtils.push(ChannelTypeEnum.EMAIL, content, "shenguangyang@foxmail.com");

        // 推送公众号
        MsgPushUtils.push(ChannelTypeEnum.WX_MP, content, "o63YF52s67WW4SQ8_VkWqVwamN-4");

        // 推送短信
        MsgPushUtils.push(ChannelTypeEnum.SMS, content, phone);
        return Resp.ok();
    }


    /**
     * 测试任务接收者类型为 通知管理员 的消息推送
     * 由于任务接收者类型为通知管理员, 所里这里配置的手机号/邮箱/微信openId无效, 由页面绑定的接收者来接收消息
     */
    @GetMapping("/testPushEmailToAdmin")
    public Resp<?> testPushEmailToAdmin() {
        MsgContent content = new MsgContent();
        content.setTaskCode("admin_email_task_test");
        content.setTitle("这是一个测试标题");
        content.setBizKey("1");
        content.setBizType("test");
        content.addTplData("data", RandomUtil.randomInt(10000, 99999));

        MsgPushUtils.push(ChannelTypeEnum.EMAIL, content, "");
        return Resp.ok();
    }

    @ApiOperation("阿里云短信推送")
    @GetMapping("/testPushAliyunSmsToAdmin")
    public Resp<?> testPushAliyunSmsToAdmin() {
        MsgContent content = new MsgContent();
        content.setTaskCode("admin_sms_task_test");
        content.setBizKey("1").setBizType("test");
        content.addTplData("data", RandomUtil.randomInt(10000, 99999));

        MsgPushUtils.push(ChannelTypeEnum.SMS, content, null);
        return Resp.ok();
    }

    @ApiOperation("测试微信公众号推送测试")
    @GetMapping("/testPushWxMpToAdmin")
    public Resp<?> testPushWxMpToAdmin() {
        MsgContent content = new MsgContent();
        content.setTaskCode("admin_wxmp_task_test");
//        content.setPageUrl("https://www.baidu.com")
        content.setBizKey("1").setBizType("test");
        content.addTplData("goodsName", "华为mate60pro 遥遥领先")
                .addTplData("time", DateUtils.getDateOfStr(new Date(), "yyyy-MM-dd"))
                .addTplData("price", "6999", "#ef5777")
                .addTplData("remark", "麒麟9000牛逼");
        content.pushAfter(Duration.ofSeconds(15));
        MsgPushUtils.pushAsync(ChannelTypeEnum.WX_MP, content, "aaa");
        return Resp.ok();
    }

}
